﻿using MessageMachine;
using System.Collections.Generic;
using System.Windows.Forms;

namespace InputWFMachine
{
    public struct ValidKey
    {
        public Keys key;
        public int msgUp;
        public int msgDown;
        public ValidKey(Keys key, int msgUp, int msgDown)
        { this.key = key; this.msgUp = msgUp; this.msgDown = msgDown; }
    }

    public class InputWF : Control
    {
        public int msgAnyKey;
        public List<ValidKey> keys;
        protected override void OnKeyDown(KeyEventArgs keyEvent)
        {
            foreach (ValidKey k in keys)
            {
                if (keyEvent.KeyCode == k.key)
                    MessagePasser.Send(new MessageMachine.Message(k.msgDown));
            }
            if (keyEvent.KeyCode != null)
                MessagePasser.Send(new MessageMachine.Message(msgAnyKey));
        }
        protected override void OnKeyUp(KeyEventArgs keyEvent)
        {
            foreach (ValidKey k in keys)
            {
                if (keyEvent.KeyCode == k.key)
                    MessagePasser.Send(new MessageMachine.Message(k.msgUp));
            }
        }

        public InputWF(int msgAnyKey, List<ValidKey> keys)
        {
            this.msgAnyKey = msgAnyKey;
            this.keys = keys;
        }
    }
}
